import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipInputStream;

public class FileDemo {
    public static void main(String[] args) throws IOException, ParseException {
//        File file = new File("src\\test\\filetest.txt");
//        System.out.println("file.length() = " + file.length());
//        File file1 = new File("F:\\code\\springbootDemo\\src\\test\\shu3.png");
//        File file2 = new File("src\\test\\shu3.png");
//        System.out.println("file1.length() = " + file1.length());
//        System.out.println("file1.length() = " + file2.length());
//        System.out.println("file1.getPath() = " + file1.getPath());
//        System.out.println("file2.getPath() = " + file2.getPath());
//        System.out.println("file2.getAbsolutePath() = " + file2.getAbsolutePath());
//        File dir = new File("F:\\ZStudy");
//        String[] list = dir.list();
//        for (int i = 0; i < list.length; i++) {
//            System.out.println( list[i]);
//        }
//        File[] files = dir.listFiles();
//        for (File file3 : files) {
//            System.out.println(file3.getAbsolutePath());
//        }
//        System.out.println(" 是否存在该文件夹 "+(dir.exists() && dir.isDirectory()) );
//        if(dir.exists() && dir.isDirectory()){
//            // 是否存在，是否是文件夹
//            File[] filess = dir.listFiles();
//            if(filess!=null && filess.length>0 ){
//            for (int i = 0; i < filess.length; i++) {
//                System.out.println("循环");
//                System.out.println("filess[i].getName() = " + filess[i].getName());
//                    if(filess[i].isFile()&& filess[i].getName().contains("eyou")){
//                        System.out.println("找到：filess[i].getAbsolutePath() = " + filess[i].getAbsolutePath());
//                        break;
//                    }
//                }
//            }
//        }
        File file = new File("src/test/filetest.txt");

//        if(file.exists() && file.isFile()){
//            InputStream is = new FileInputStream(file);
//            byte [] buffer = new byte[2];
//            int len ;
////            while ((len=is.read(buffer))!=-1){
////                String rs = new String(buffer,0,len);
////                System.out.println("len = " + rs);
////            }
//            while ((len=is.read())!=-1){
////                String rs = new String(buffer,0,len);
//                int rs = len;
//                System.out.println("len = " + rs);
//            }
//            O
//            OutputStream os = new FileOutputStream(file);
//            os.write('a');
//            os.write('b');
//            os.write('c');
//            os.close();
//    }
//        testFile01();
//        testFile02();
//        testFile03();
//        testFile04();

//        File file1 = new File("F:\\ZStudy");
//        File file2 = new File("src/main/ZStudy1");
//        System.out.println("file2.isDirectory() = " + file2.isDirectory());
//        if(file1.exists()&&file1.isDirectory()){
//            File[] files = file1.listFiles();
//            file2.mkdirs();
//            if(files!=null && files.length>0) {
//                for (int i = 0; i < files.length; i++) {
//                    System.out.println("files[i].getAbsolutePath() = " + files[i].getAbsolutePath());
//                    System.out.println("file2+files[i].getName() = " + file2 + files[i].getName());
//                    if(files[i].isFile()){
//                        try(
//                                InputStream is = new FileInputStream(files[i].getAbsolutePath());
//                                BufferedInputStream bis = new BufferedInputStream(is);
//                                OutputStream os = new FileOutputStream(file2+"\\"+files[i].getName());
//                                BufferedOutputStream bos = new BufferedOutputStream(os);
//                                ){
//                            byte []buffer = new byte[1024];
//                            int len ;
//                            while ((len = bis.read(buffer))!=-1){
//                                bos.write(buffer,0,len);
//                            }
//                        }
//                    }else{
//                        File file3 = new File(file2+"\\"+files[i].getName());
//                        // 这里需要重新调用for循环，执行文件对象的调用，否则只是复制文件目录
//                        file3.mkdir();
//                    }
//
//                }
//            }
//        }

    }

    private static void testFile04() {
        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream("src/test/filetest.txt");
                BufferedInputStream bis = new BufferedInputStream(is);
                OutputStream os = new FileOutputStream("src/test/filetest_04.txt");
                BufferedOutputStream bos = new BufferedOutputStream(os)
        ){
            int len = 0;
            byte []buffer = new byte[1024];
            while ((len = bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("缓冲低级字节数组= " + (end-start) / 1000.0);
    }

    private static void testFile03() {
        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream("src/test/filetest.txt");
                BufferedInputStream bis = new BufferedInputStream(is);
                OutputStream os = new FileOutputStream("src/test/filetest_03.txt");
                BufferedOutputStream bos = new BufferedOutputStream(os)
        ){
            int len = 0;
            while ((len = bis.read())!=-1){
                bos.write(len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("缓冲低级字节= " + (end-start) / 1000.0);
    }

    private static void testFile02() {
        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream("src/test/filetest.txt");
                OutputStream os = new FileOutputStream("src/test/filetest_02.txt")
        ){
            int len = 0;
            byte buffer[]  = new byte[1024];
            while ((len = is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("低级字节数组= " + (end-start) / 1000.0);

    }

    private static void testFile01() {

        long start = System.currentTimeMillis();
        try(
                InputStream is = new FileInputStream("src/test/filetest.txt");
                OutputStream os = new FileOutputStream("src/test/filetest_01.txt")
                ){
            int len = 0;
            while ((len = is.read())!=-1){
                os.write(len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("低级字节流= " + (end-start) / 1000.0);
    }
}
